package com.rich.dmr.widget;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

public class DataBaseUtils {
    private String TAG = "rich---DataBaseUtils";
    private static DataBaseUtils mDataBaseUtils;
    private ContentResolver mContentResolver;
    Uri channelUri = Uri.parse("content://com.provider.dmr/channel_list");
    private Uri dmrUri = Uri.parse("content://com.provider.dmr/dmr_contact");

    public static DataBaseUtils getInstance(Context context) {
        if (mDataBaseUtils == null) {
            synchronized (DataBaseUtils.class) {
                mDataBaseUtils = new DataBaseUtils(context);
            }
        }
        return mDataBaseUtils;
    }
    private DataBaseUtils(Context context) {
        mContentResolver = context.getContentResolver();
    }

    public ChannelModel findChannel(int txChannel,int rxChannel,int colorCode,int timeSlot,int power,int channelMode,String zone,int password) {
        Cursor cursor = mContentResolver.query(channelUri, null, "tx_fre=? and rx_fre=? and color_code=? and time_slot=? and power=? and zone=? and password=? and channel_mode=?",
			new String[]{String.valueOf(txChannel),String.valueOf(rxChannel),String.valueOf(colorCode),
			String.valueOf(timeSlot),String.valueOf(power),String.valueOf(zone),String.valueOf(password),String.valueOf(channelMode)}, null);
        ChannelModel channelModel = null;
        if (cursor != null) {
            while (cursor.moveToNext()) {
                int id = cursor.getInt(cursor.getColumnIndex("_id"));
                String channelName = cursor.getString(cursor.getColumnIndex("channel_name"));

                int txFre= cursor.getInt(cursor.getColumnIndex("tx_fre"));
                int rxFre = cursor.getInt(cursor.getColumnIndex("rx_fre"));
                int colorC = cursor.getInt(cursor.getColumnIndex("color_code"));
                int timeS = cursor.getInt(cursor.getColumnIndex("time_slot"));
                int powerS = cursor.getInt(cursor.getColumnIndex("power"));
                int phone = cursor.getInt(cursor.getColumnIndex("phone"));
                int passwordS = cursor.getInt(cursor.getColumnIndex("password"));
                String zoneS = cursor.getString(cursor.getColumnIndex("zone"));
                int channelM = cursor.getInt(cursor.getColumnIndex("channel_mode"));
                channelModel = new ChannelModel(id,channelName, txFre,rxFre,colorC,timeS,powerS,passwordS,zoneS,channelM);
            }
            cursor.close();
        }
        return channelModel;
    }

    public String findContactByPhone(int phone,int callType) {
        Cursor cursor = mContentResolver.query(dmrUri, null, "phone=? and call_type=?", new String[]{String.valueOf(phone),String.valueOf(callType)}, "name asc");
        String name = null;
        if (cursor != null) {
            while (cursor.moveToNext()) {
                 name = cursor.getString(cursor.getColumnIndex("name"));
                 Log.d(TAG, "name =" + name);
            }
            cursor.close();
        }
        return name;
    }

}
